/**
 * 
 */
package name.webdizz.gadget.four.envelope.client.view.widgets;

import name.webdizz.gadget.four.envelope.client.ImageBundleFactory;
import name.webdizz.gadget.four.envelope.client.domain.ExpenseItem;

import com.google.gwt.dom.client.SpanElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.TextBox;

/**
 * @author Izzet_Mustafayev
 * 
 */
public abstract class BaseExpenseListItemUi extends Composite {
	@UiField
	protected SpanElement expenseValue;

	@UiField
	protected SpanElement expenseName;

	@UiField
	protected Image editIcon;

	@UiField
	protected TextBox expenseValueEdit;

	protected ExpenseItem expenseItem;

	public BaseExpenseListItemUi() {
		super();
	}

	/**
	 * @param expenseItem
	 */
	protected void init(final ExpenseItem expenseItem) {
		this.expenseItem = expenseItem;
		expenseName.setInnerText(expenseItem.getName());
		if (expenseItem.getPlanned().equals(expenseItem.getActual())
				|| expenseItem.getActual().intValue() == 0) {
			expenseValue.setInnerText(expenseItem.getPlanned().toString());
			expenseValueEdit.setValue(expenseItem.getPlanned().toString());
		} else {
			expenseValue.setInnerText(expenseItem.getActual().toString());
			expenseValueEdit.setValue(expenseItem.getActual().toString());
		}
		expenseValueEdit.setVisible(false);
		expenseValueEdit.setName(expenseItem.getIdentity().toString());
		editIcon.setUrl(ImageBundleFactory.createEnvelopeImageBundle()
				.editIcon().getURL());
		// show input to modify expense value
		editIcon.addClickHandler(new ClickHandler() {
			public void onClick(final ClickEvent event) {
				expenseValueEdit.setVisible(true);
				expenseValueEdit.setFocus(true);
			}
		});
	}

	protected abstract void assignExpenseValueChangeHandler();
}
